﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

public partial class Reports_BI_KPI_MainPanel : System.Web.UI.Page
{

    #region Members

    string nl = @"
";
    DataTable tblData;
    StringBuilder sb = new StringBuilder();
    DateTime dt = DateTime.Now;
    int rowCount = 0;
    int rowAll = 0;
    int MonthStart = 1;
    int MonthStop = 1;
    int TurnoverFull = 0;
    int PlanFull = 0;

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        Exception ret = null;
        ret = MakeData();
        if (ret != null)
        {
            oErr.Text = "<p>" + ret.Message + "</p>";
            return;
        }
        ret = MakeHTML();
        if (ret != null)
        {
            oErr.Text = "<p>" + ret.Message + "</p>";
            return;
        }
    }

    private void MakeDataTable()
    {
        tblData = new DataTable("Chart");
        tblData.Columns.Add("Секция", typeof(string));
        tblData.Columns.Add("Дата", typeof(DateTime));
        tblData.Columns.Add("Оборот", typeof(int));
        tblData.Columns.Add("План", typeof(int));
        tblData.Columns.Add("Место", typeof(string));
    }

    private Exception MakeData()
    {
        Exception ret = null;
        dt = new DateTime(dt.Year, dt.Month, dt.Day);
        MonthStart = 1;
        MonthStop = dt.Month;
        MakeDataTable();
        ret = FillDataTable();
        if (ret != null) return ret;
        return ret;
    }

    Exception FillDataTable()
    {

        Exception ret = null;
        rowAll = 0;
        rowCount = 0;
        for (int i = MonthStart; i <= MonthStop; i++)
        {
            TurnoverFull = 0;
            PlanFull = 0;
            DataTable tblSrc = new DataTable("tbl");
            Exception err = new Ko2.DB.BI.KPI.MainPanel().Get(ref tblSrc, dt.Year, i);
            if (err != null)
            {
                ret = new Exception(err.Message, err);
                return ret;
            }
            foreach (DataRow r in tblSrc.Rows)
            {
                if (r["Name"] == DBNull.Value || r["Name"].ToString() == "Все")
                    continue;
                DataRow newRow = tblData.NewRow();
                newRow["Дата"] = new DateTime(dt.Year, i, 1);
                newRow["Секция"] = "";
                if (r["Name"] != DBNull.Value)
                {
                    newRow["Секция"] = r["Name"];
                    newRow["Место"] = r["Name"];
                }
                newRow["Оборот"] = 0;
                if (r["Turnover"] != DBNull.Value)
                {
                    newRow["Оборот"] = Convert.ToInt32(r["Turnover"]);
                    TurnoverFull += Convert.ToInt32(r["Turnover"]);
                }
                newRow["План"] = 0;
                if (r["Plan"] != DBNull.Value)
                {
                    newRow["План"] = Convert.ToInt32(r["Plan"]);
                    PlanFull += Convert.ToInt32(r["Plan"]);
                }

                tblData.Rows.Add(newRow);
                rowAll++;
            }
        }

        return ret;
    }

    Exception MakeHTML()
    {
        Exception ret = null;
        sb.Append(
@"
        <script language='javascript'>
//<![CDATA[
    function drawVisualization() {
        var data = new google.visualization.DataTable();");
        sb.Append(@"data.addRows(" + rowAll + ");");

        sb.Append(@"
data.addColumn('string', 'Секция');
data.addColumn('date', 'Дата');
data.addColumn('number', 'Оборот');
data.addColumn('number', 'План');
data.addColumn('string', 'Место');");

        foreach (DataRow r in tblData.Rows)
            AddNode(ref sb, (string)r["Секция"], (DateTime)r["Дата"], (int)r["Оборот"], (int)r["План"], (string)r["Место"]);

        sb.Append(@"
        var motionchart = new google.visualization.MotionChart(
        document.getElementById('The_Chart'));
        motionchart.draw(data, { 'width': 800, 'height': 400 });
    }

    google.setOnLoadCallback(OnLoad);
//]]>
</script>");

        oBody.Text = sb.ToString();
        return ret;
    }

    void AddNode(ref StringBuilder parSB, string Name, DateTime dt, int val1, int val2, string Location)
    {
        string dtStr = String.Format("{0},{1},{2}", dt.Year, dt.Month - 1, dt.Day - 1);
        parSB.Append(
@"
data.setValue(" + rowCount + ", 0, '" + Name + "');" + nl
+ @"data.setValue(" + rowCount + ", 1, new Date(" + dtStr + @"));" + nl
+ @"data.setValue(" + rowCount + ", 2, " + val1 + @");" + nl
+ @"data.setValue(" + rowCount + ", 3, " + val2 + @");" + nl
+ @"data.setValue(" + rowCount + ", 4, '" + Location + @"');" + nl
);
        rowCount++;
    }    

}